package 位运算;

public class 比特位计数2 {
    //https://leetcode.cn/problems/counting-bits/description/
    public int[] countBits(int n) {
        int[] ret = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            int sum = 0, tmp = i;
            while (tmp != 0) {
                tmp &= (tmp - 1);
                sum++;
            }
            ret[i] = sum;
        }
        return ret;
    }
}
